package com.hzp.tool.mybatis.custom;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;

import java.io.Serializable;
import java.util.List;

/**
 * @author Huang Zhaoping
 * @date 2019/2/15
 */
public class MyGeneratePlugin extends PluginAdapter {
    private static String modelSuffix;
    private static String mapperSuffix;
    private static boolean enableSerial;
    private static boolean enableLombok;
    private static boolean enableSwagger;

    public static void setModelSuffix(String modelSuffix) {
        MyGeneratePlugin.modelSuffix = modelSuffix;
    }

    public static void setMapperSuffix(String mapperSuffix) {
        MyGeneratePlugin.mapperSuffix = mapperSuffix;
    }

    public static void setEnableSerial(boolean enableSerial) {
        MyGeneratePlugin.enableSerial = enableSerial;
    }

    public static void setEnableLombok(boolean enableLombok) {
        MyGeneratePlugin.enableLombok = enableLombok;
    }

    public static void setEnableSwagger(boolean enableSwagger) {
        MyGeneratePlugin.enableSwagger = enableSwagger;
    }

    @Override
    public boolean validate(List<String> list) {
        return true;
    }

    @Override
    public void initialized(IntrospectedTable introspectedTable) {
        super.initialized(introspectedTable);
        if (modelSuffix != null && modelSuffix.length() > 0) {
            introspectedTable.setBaseRecordType(introspectedTable.getBaseRecordType() + modelSuffix);
        }
        if (mapperSuffix != null && mapperSuffix.length() > 0) {
            introspectedTable.setMyBatis3JavaMapperType(introspectedTable.getMyBatis3JavaMapperType().replaceAll("Mapper$", mapperSuffix));
            introspectedTable.setMyBatis3XmlMapperFileName(introspectedTable.getMyBatis3XmlMapperFileName().replaceAll("Mapper\\.", mapperSuffix + "."));
        }
    }

    @Override
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        if (enableSerial) {
            topLevelClass.addSuperInterface(new FullyQualifiedJavaType(Serializable.class.getName()));
        }
        if (enableSwagger) {
            topLevelClass.addImportedType("io.swagger.annotations.ApiModelProperty");
        }
        if (enableLombok) {
            topLevelClass.addImportedType("lombok.Data");
            topLevelClass.addAnnotation("@Data");
        }
        return super.modelBaseRecordClassGenerated(topLevelClass, introspectedTable);
    }

    @Override
    public boolean modelFieldGenerated(Field field, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        if (enableSwagger) {
            String remark = introspectedColumn.getRemarks();
            if (remark != null && (remark = remark.trim()).length() > 0) {
                field.addAnnotation("@ApiModelProperty(\"" + remark + "\")");
            }
        }
        return super.modelFieldGenerated(field, topLevelClass, introspectedColumn, introspectedTable, modelClassType);
    }

    @Override
    public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        if (enableLombok) return false;
        return super.modelGetterMethodGenerated(method, topLevelClass, introspectedColumn, introspectedTable, modelClassType);
    }

    @Override
    public boolean modelSetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        if (enableLombok) return false;
        return super.modelSetterMethodGenerated(method, topLevelClass, introspectedColumn, introspectedTable, modelClassType);
    }
}
